<?php

namespace app\api\controller;

use app\common\controller\Api;
use GatewayWorker\Lib\Gateway;
use think\Db;

/**
 * 示例接口
 */
class Play extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
//    protected $noNeedLogin = ['test', 'test1'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function join()
    {
        $param=$this->request->param();
        Db::name('game')->insert([
            'user_id'=>$this->auth->id,
            'room_id'=>$param['room_id'],
            'createtime'=>time()
        ]);

        $client_id=Gateway::getClientIdByUid($this->auth->id);
        //绑定群组
        if($client_id){
            foreach($client_id as $k=>$v){
                Gateway::joinGroup($v, $param['room_id']);
            }
        }
        $user=Db::name('user')->where('id',$this->auth->id)->find();
        //通知群组
        $msg=[
            'type'=>'join',
            'id'=>$this->auth->id,
            'avatar'=>cdnurl($user['avatar'],true),
        ];
        $msg=json_encode($msg);
        Gateway::sendToGroup($param['room_id'], $msg);
        $this->success();
    }
}
